package com.huawei.hianalytics.log.e;

import android.text.TextUtils;
import com.huawei.hianalytics.util.d;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class b {
    public static String a(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        String str2 = "";
        try {
            MessageDigest instance = MessageDigest.getInstance(AaidIdConstant.SIGNATURE_SHA256);
            instance.update(str.getBytes("UTF-8"));
            return d.a(instance.digest());
        } catch (NoSuchAlgorithmException e) {
            com.huawei.hianalytics.g.b.d("LogCrypter", "getSHA256StrJava, getInstance - No such algorithm,transformation!");
            return str2;
        } catch (UnsupportedEncodingException e2) {
            com.huawei.hianalytics.g.b.d("LogCrypter", "getSHA256StrJava, getBytes - Unsupported coding format!");
            return str2;
        }
    }

    public static String a(Key key) {
        return d.a(key.getEncoded());
    }

    public static Cipher a(int i, Key key, byte[] bArr) {
        try {
            AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(bArr);
            Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
            instance.init(i, key, ivParameterSpec);
            return instance;
        } catch (NoSuchPaddingException e) {
            com.huawei.hianalytics.g.b.c("LogCrypter", "AES:getCipher() No such filling parameters !");
        } catch (NoSuchAlgorithmException e2) {
            com.huawei.hianalytics.g.b.c("LogCrypter", "AES:getCipher() getInstance - No such algorithm,transformation!");
        } catch (InvalidAlgorithmParameterException e3) {
            com.huawei.hianalytics.g.b.c("LogCrypter", "AES:getCipher() Invalid algorithm parameters");
        } catch (InvalidKeyException e4) {
            com.huawei.hianalytics.g.b.c("LogCrypter", "AES:getCipher() init - Invalid key!");
        }
        return null;
    }

    public static byte[] a() {
        byte[] bArr = new byte[16];
        new SecureRandom().nextBytes(bArr);
        return bArr;
    }

    public static Key b() {
        byte[] bArr = new byte[16];
        new SecureRandom().nextBytes(bArr);
        return new SecretKeySpec(bArr, "AES");
    }
}
